package com.edu.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.edu.entity.CrmBanner;
import com.edu.service.CrmBannerService;
import com.edu.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 首页banner表 前端控制器
 * 后台banner后台控制器
 * </p>
 *
 * @author mxs
 * @since 2021-11-09
 */
@RestController
@RequestMapping("/educms/admin")
@CrossOrigin
public class BannerAdminController {
    @Autowired
    private CrmBannerService bannerService;

    /**
     * 分页查询banner
     *
     * @param pageNo
     * @param limit
     * @return
     */
    @GetMapping("/pageBanner/{pageNo}/{limit}")
    public R pageBanner(@PathVariable long pageNo,
                        @PathVariable long limit) {
        Page<CrmBanner> page = new Page<>(pageNo, limit);
        IPage<CrmBanner> bannerPage = bannerService.page(page, null);
        return R.success().data("records", bannerPage.getRecords()).data("total", bannerPage.getTotal());
    }

    /**
     * 更新banner
     *
     * @param banner
     * @return
     */
    @PutMapping("/updateBanner")
    public R updateBanner(@RequestBody(required = true) CrmBanner banner) {
        bannerService.updateById(banner);
        return R.success();
    }

    /**
     * 添加
     *
     * @param banner
     * @return
     */
    @PostMapping("/addBanner")
    public R addBanner(@RequestBody(required = true) CrmBanner banner) {
        bannerService.save(banner);
        return R.success();
    }

    /**
     * 根据id删除banner
     *
     * @param id
     * @return
     */
    @DeleteMapping("/deleteBanner/{id}")
    public R deleteBanner(@PathVariable String id) {
        bannerService.removeById(id);
        return R.success();
    }

    @GetMapping("/getBanner/{id}")
    public R getBannerById(@PathVariable String id) {
        CrmBanner banner = bannerService.getById(id);
        return R.success().data("banner",banner);
    }


}





















